<?php
class Authentication_model extends CI_Model {
    
    function __construct()
    {
        parent::__construct();
    }
    function verifyLogin($user,$password)
    {
        $this->db->where('ten_nguoidung',$user);
        $userData=$this->db->get('nguoidung');
        if($userData->num_rows()!=0)
            {
                $this->db->where('ten_nguoidung',$user);
                $this->db->where('matkhau_nguoidung',md5($password));
                $AllData=$this->db->get('nguoidung');
                if($AllData->num_rows()!=0) 
                    {
                        foreach($AllData->result_array() as $row);
                        $userID=$row['id_nguoidung'];
                        $data = array(
                           'userID'  => $userID,
                           'user'    => $row['ten_nguoidung'],
                           'name'    => $row['tenthat_nguoidung'],
                           'level'   => $row['capnguoidung'],
                           'logged_in' => TRUE
                            );
                        $this->session->set_userdata($data);
                        return true;
                    } 
            }
            return false;
    }
    function checkAcess($idquyen,$level=null)
        {
            $useId=$this->session->userdata('userID');
            $this->db->where('id_nguoidung',$useId);
            $this->db->where('id_quyen',$idquyen);
            $query=$this->db->get('nguoidung_quyen');
            if($query->num_rows()!=0) return true;
            else 
            {
             header('Location:'.site_url('home'));
             exit;
            }
        }
    function checkLogin()
    {
        $isLogged=$this->session->userdata('logged_in');
        if($isLogged!=TRUE) header("Location:".site_url('login'));
    }
    function logout()
    {
        $this->session->sess_destroy();
        header('location:'.site_url('login'));
        exit;
    }
    function dsQuyen($userID)
    {
        
    }
}
?>